<template>
  <div class="header">
    <div class="lf">
      <el-avatar :src="userStore.avatar" :size="70"></el-avatar>
      <div class="user-info">
        <div class="name">{{ userStore.name }}</div>
        <div class="info-item">
          <span class="info-label">当前等级：</span>
          <span class="info-val">{{ userInfo.memberLevelName }}</span>
        </div>
        <div class="info-item">
          <span class="info-label">当前成长值：</span>
          <span class="info-val">{{ userInfo.growth }}</span>
        </div>
      </div>
    </div>
    <div class="rt">
      <div class="p-list">
        <div class="p-item">
          <div class="p-val-box">
            <span class="p-val">{{ userInfo.integration }}</span>
            <span class="p-unit">分</span>
          </div>
          <div class="p-label">我的积分</div>
        </div>
        <div class="p-item">
          <div class="p-val-box">
            <span class="p-val">{{ userInfo.couponCount }}</span>
            <span class="p-unit">次</span>
          </div>
          <div class="p-label">已兑换</div>
        </div>
      </div>
    </div>
  </div>
  <div class="list-container">
    <el-row :gutter="20">
      <el-col :span="8" v-for="item in couponList" :key="item.id">
        <div class="c-card">
          <div class="cut"></div>
          <div class="info">
            <div class="c-name">{{ item.couponName }}</div>
            <div class="c-note">{{ item.note }}</div>
            <div class="pic">
              <el-image
                style="width: 120px; height: 120px"
                :src="baseUrl + item.couponImg"
                fit="cover"
              />
            </div>
          </div>
          <div class="value">
            <div class="num">
              {{ item.amount }} 元
              <span class="limit" v-if="item.minPoint === 0.0">无门槛</span>
              <span class="limit" v-else>满 {{ item.minPoint }} 元</span>
            </div>
            <div class="time">{{ item.memberLevelName }}</div>
            <div class="time">开始时间：{{ item.startTime }}</div>
            <div class="time">截止时间：{{ item.endTime }}</div>
          </div>
          <div class="point">
            <span style="color: #67c23a" v-if="item.requiredIntegral === 0"
              >免费</span
            >
            <span class="price" v-else>{{ item.requiredIntegral }} 积分</span>
            <p style="font-size: 12px">{{ item.receiveCount }} 人已兑换</p>
          </div>
          <div class="action">
            <el-button
              size="small"
              color="#ED5564"
              v-if="!item.enable"
              disabled="true"
            >
              <el-tooltip
                class="box-item"
                effect="dark"
                :content="item.msg"
                placement="top"
              >
                <span style="color: #fff">立即兑换</span>
              </el-tooltip>
            </el-button>
            <el-button size="small" color="#ED5564" v-else>
              <span style="color: #fff" @click="handleExchange(item)"
                >立即兑换</span
              >
            </el-button>
          </div>
        </div>
      </el-col>
    </el-row>
  </div>
</template>
<script setup>
import { listCoupon, addCoupon } from "@/api/mall/client/points/coupon";
import useUserStore from "@/store/modules/user";
import { getCurrentInstance } from "vue";
import { getUserMemberInfo } from "@/api/mall/client/user/user";
const userStore = useUserStore();
const { proxy } = getCurrentInstance();
const baseUrl = import.meta.env.VITE_APP_BASE_API;
const couponList = ref([]);
const queryParams = ref({
  pageNum: 1,
  pageSize: 6,
});
const getCouponList = async () => {
  const res = await listCoupon(queryParams.value);
  couponList.value = res.rows;
};

getCouponList();
// 兑换优惠券
const handleExchange = (item) => {
  if (item.enable) {
    addCoupon({ couponId: item.id, code: item.code }).then((res) => {
      proxy.$modal.msgSuccess("兑换成功");
      getCouponList();
    });
  } else {
    proxy.$modal.msgError(item.msg);
  }
};

// 用户信息
const userInfo = ref({});
const getMemberInfo = async () => {
  const res = await getUserMemberInfo();
  userInfo.value = res.data;
};
getMemberInfo();
</script>

<style lang="scss" scoped>
.header {
  display: flex;
  background-color: #fff2da;
  border: solid 1px #fed4ae;
  overflow: hidden;
  justify-content: space-between;
  align-items: center;
  padding: 20px 20px;
  margin-top: 10px;
  margin-bottom: 20px;
  .lf {
    display: flex;
    width: 200px;
    .user-info {
      color: #666;
      margin-left: 10px;
      .name {
        font: 600 14px/20px "microsoft yahei";
        margin-bottom: 6px;
      }
      .info-item {
        line-height: 22px;
        font-size: 12px;
        .info-val {
          font-weight: 600;
        }
      }
    }
  }
  .rt {
    flex: 1;
  }
  .p-list {
    display: flex;
    justify-content: flex-end;
    .p-item {
      width: 100px;
      .p-val-box {
        font: 12px/30px "microsoft yahei";
        color: #ff8400;
        text-align: center;
      }
      .p-val {
        font-size: 18px;
      }
      .p-unit {
        font-size: 14px;
      }
      .p-label {
        font: 12px/20px "microsoft yahei";
        color: #555;
        text-align: center;
      }
    }
  }
}
.c-card {
  background-color: #fffbf3;
  display: block;
  height: 220px;
  border: dotted 1px #ffcfbf;
  position: relative;
  z-index: 1;
  margin-bottom: 10px;
  cursor: pointer;
  .cut {
    background: url(https://www.chawo.com/shop/templates/default/images/point_bg.png)
      no-repeat -140px 0;
    width: 20px;
    height: 18px;
    position: absolute;
    z-index: 2;
    top: -9px;
    left: 5px;
  }
  .info {
    height: 217px;
    padding: 20px 30px 0 25px;
    position: absolute;
    z-index: 1;
    top: 0;
    left: 0;
    font-size: 12px;
    background: url(https://www.chawo.com/shop/templates/default/images/point_voucher_bg.png)
      repeat-y right 0;
    .c-name {
      font-weight: 600;
      text-align: center;
      display: block;
      height: 20px;
      overflow: hidden;
      color: #333;
    }
    .c-note {
      text-align: center;
      height: 20px;
      margin-bottom: 5px;
      color: #666;
    }
  }
  .value {
    padding-top: 5px;
    padding-bottom: 5px;
    display: block;
    padding-left: 185px;

    .num {
      font: 30px/48px "microsoft yahei";
      color: #d93600;
      height: 48px;
    }
    .limit,
    .time {
      color: #d83500;
      white-space: nowrap;
      margin-bottom: 4px;
      font-size: 12px;
    }
    .time {
      color: #999;
    }
  }
  .point {
    background-color: #fff2da;
    height: 56px;
    padding-top: 5px;
    padding-bottom: 5px;
    display: block;
    padding-left: 185px;
    .price {
      font: 16px/24px "microsoft yahei";
      color: #d93600;
      height: 24px;
    }
    p {
      line-height: 20px;
      display: block;
      color: #777;
      height: 20px;
    }
  }
  .action {
    padding-top: 10px;
    display: block;
    padding-left: 185px;
  }
}
</style>
